<template>
  <div class="login">
    <div class="login-item">
      <div class="login-item-title">邮箱</div>
      <div class="login-item-info">
        <input placeholder="请输入邮箱" type="text" autocomplete="off" v-model="email"/>
        <!---->
      </div>
    </div>
    <!---->
    <div class="login-item">
      <div class="login-item-title">设置密码</div>
      <div class="login-item-info">
        <input placeholder="请设置账号密码" autocomplete="off" :type="this.flag? 'password':'text'" v-model="pwd1"/>
        <div @click="show" class="login-item-info-icon"><i :class="flag ?'iconfont icon-close-eye':'iconfont icon-yanjing'"></i></div>
        <!---->
      </div>
    </div>
    <div class="login-item">
      <div class="login-item-title">确认密码</div>
      <div class="login-item-info">
        <input placeholder="请确认您的账号密码" autocomplete="off" :type="this.flag? 'password':'text'"  v-model="pwd2"/>
        <div @click="show" class="login-item-info-icon"><i :class="flag ?'iconfont icon-close-eye':'iconfont icon-yanjing'"></i></div>
        <!---->
      </div>
    </div>
    <button class="to-login" @click="emailregister">注册邮箱账号</button>
    <div @click="back" class="login-by-password">
      <i class="iconfont iconfont-arrow-left"></i>
      返回登录
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { emailregister } from '@/api'
import { Toast } from 'vant'
Vue.use(Toast)
export default {
  data () {
    return {
      flag: true,
      email: '',
      pwd1: '',
      pwd2: ''
    }
  },
  methods: {
    back () {
      this.$router.go(-1)
    },
    show () {
      this.flag = !this.flag
    },
    emailregister () {
      const emailreg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
      if (!this.email) {
        Toast('请输入邮箱')
        return
      }
      if (!emailreg.test(this.email)) {
        Toast('邮箱格式不正确')
        return
      }
      if (this.pwd1 !== this.pwd2) {
        Toast('两次密码不一样')
        return
      }
      emailregister({ email: this.email, password: this.pwd2 }).then(res => {
        console.log(res.data)
        if (res.data.code === '10666') {
          Toast('注册成功')
          this.$router.push('/login')
        } else if (res.data.code === '10606') {
          Toast('该用户已注册')
        } else if (res.data.code === '10400') {
          Toast('请完善表单信息')
        }
      })
    }
  }
}
</script>

<style lang="scss">
@import '@/lib/reset.scss';
.login {
  padding: {
    left: 20px;
    right: 20px;
  };
  &-item {
    font-size: 12px;
    &-title{}
    &-info{
      margin-top: 5px;
      margin-bottom: 20px;
      border-bottom: 1px solid #ececec;
      input {
        font-size: 16px;
        flex: 1;
        height: 40px;
        outline: 0;
        border: 0;
      }
      display: flex;
      &-btn{
        color: #ff734c;
        line-height: 40px;
      }
      &-btn2{
        color: #71797f;
        line-height: 40px;
      }
      &-icon {
        display: flex;
        align-items: center;
        margin-right: 10px;
        i {
          font-size: 10px;
        }
      }
    }
  }
  .to-login {
    @include rect(100%,0.42rem);
    margin-top: 10px;
    border: 0;
    outline: 0;
    background-color: #ff734c;
    font-size: 16px;
    color: #fff;
    border-radius: 0.21rem;
  }
  .login-by-password {
    margin-top: 30px;
    text-align: center;
    font-size: 17px;
    color: #71797f;
  }
}
</style>
